<?php

// Line Count
//  Args:  list of file specs, possibly with wildcards

// $argv[] = command line arguments
// $argc   = # args (including $argv[0] == "linecnt.php")


// Set the locale info so we get the correct thousands separator
setlocale (LC_ALL, '');

$TotLines =
$TotBytes =
$TotFiles = 0;

// Loop through the file patterns
for ($i = 1; $i < $argc; $i++)
{
    // Split apart the file directory name and base
    $FileDir  = dirname  ($argv[$i]);
    $FileBase = basename ($argv[$i]);

    // Get a handle to the directory
    $hDir = opendir ($FileDir);

    // Loop through the files in the dir
    LoopFiles ($FileDir, $FileBase, $hDir);

    // We no longer need this resource
    closedir ($hDir); $hDir = false;
} // End FOR

echo ("---------------------------------------------------------\r\n");

// Display the total stats
DisplayStats ($TotLines, $TotBytes, $TotFiles, false);

exit (0);


//***************************************************************************
//  LoopFiles
//
//  Loop through the list of files
//***************************************************************************

function LoopFiles ($FileDir, $FileBase, $hDir)

{
    global $TotLines, $TotBytes, $TotFiles;

    $LclLines =
    $LclBytes =
    $LclFiles = 0;

    // If the file dir doesn't end with a backslash, do so now
    if (substr ($FileDir, -1, 1) != "\\")
        $FileDir .= "\\";

    // Loop through the files in the dir
    while (($FileName = readdir ($hDir)) !== false)
    {
        if ($FileName != '.'
         && $FileName != '..'
         && fnmatch ($FileBase, $FileName, FNM_CASEFOLD))
        {
            // Combine the file directory and name
            $FilePath = $FileDir . $FileName;

            // Read in the file contents
            $FileNew = file ($FilePath, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
            if ($FileNew == false)
            {
                echo "***Unable to read file <$FilePath>\r\n";
                continue;
            } // End IF

            // Accumulate local stats
            $LclLines += count ($FileNew);
            $LclBytes += filesize ($FilePath);
            $LclFiles++;

            $FileNew = false;   // Free the storage
        } // End IF
    } // End WHILE

    // Display the local stats
    DisplayStats ($LclLines, $LclBytes, $LclFiles, $FileBase);

    // Accumulate total stats
    $TotLines += $LclLines;
    $TotBytes += $LclBytes;
    $TotFiles += $LclFiles;
} // End LoopFiles


//***************************************************************************
//  DisplayStats
//
//  Display the statistics
//***************************************************************************

function DisplayStats ($TotLines, $TotBytes, $TotFiles, $FileBase)

{
    $FmtLines = CommaFmt ($TotLines) . " line" . (($TotLines == 1) ? "" : "s");
    $FmtBytes = CommaFmt ($TotBytes) . " byte" . (($TotBytes == 1) ? "" : "s");
    $FmtFiles = CommaFmt ($TotFiles) . " file" . (($TotFiles == 1) ? "" : "s");

    if ($FileBase === false)
        $FileBase = "";
    else
        $FileBase = ":  " . $FileBase;

    echo (overtake (-13, $FmtLines)
        . overtake (-23, $FmtBytes)
        . overtake (-13, $FmtFiles)
        . $FileBase
        . "\r\n"
         );
} // End DisplayStats


//***************************************************************************
//  overtake
//
//  Emulate the overtake function in APL
//***************************************************************************

function overtake ($Len, $Str)

{
    return sprintf ("%" . (-$Len) . "s", $Str);
} // End overtake


//***************************************************************************
//  CommaFmt
//
//  Format an integer with thousands separator
//***************************************************************************

function CommaFmt ($FileSize)

{
    $LocaleInfo = localeconv ();
    return number_format ($FileSize, 0, "", $LocaleInfo['thousands_sep']);
} // End CommaFmt


?>
